package com.py.payment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.payment.dto.param.MonitorParamDto;
import com.py.payment.dto.ret.LineChartRetDto;
import com.py.payment.dto.ret.MonitorRetDto;
import com.py.payment.dto.ret.PointPositionRetDto;
import com.py.payment.entity.MonitoringPoints;
import com.py.payment.entity.PointPosition;
import com.py.payment.entity.PointThreshold;
import com.py.payment.service.MonitorService;
import com.py.payment.service.MonitoringPointsService;
import com.py.payment.service.PointPositionService;
import com.py.payment.service.PointThresholdService;
import com.py.payment.tools.ToolsUtils;
import com.py.payment.tools.date.DateUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预报
 */
@RestController
@RequestMapping("/prediction")
@CrossOrigin(origins = "*")
public class PredictionController {

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private MonitoringPointsService monitoringPointsService;

    @Autowired
    private PointPositionService pointPositionService;

    @Autowired
    private PointThresholdService pointThresholdService;

    @PostMapping(name = "折线图数据",path = "/getLineChartValue")
    public ResponseData getLineChartValue(@RequestBody MonitorParamDto monitorParamDto){
        monitorParamDto.setStartTime(DateUtils.dateToDateStr(DateUtils.zeroTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
        monitorParamDto.setEndTime(DateUtils.dateToDateStr(DateUtils.endTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
        monitorParamDto.setOrderBy(2);
        List<MonitorRetDto> monitorRetList = this.monitorService.getPredictionList(monitorParamDto);
        PointPosition position = this.pointPositionService.getById(monitorParamDto.getMonitorType());
        LineChartRetDto lineChartRetDto = new LineChartRetDto();
        List<String> xData = new ArrayList<>();
        List<String> yData = new ArrayList<>();
        monitorRetList.forEach(item->{
            String x = DateUtils.dateToDateStr(item.getCreateTime(),DateUtils.HH_MM);
            String y = item.getActual().setScale(3, RoundingMode.HALF_EVEN).toString();
            xData.add(x);
            yData.add(y);
        });
        lineChartRetDto.setXData(xData);
        lineChartRetDto.setYData(yData);
        lineChartRetDto.setUnit(position.getUnit());
        return ResponseData.success(lineChartRetDto);
    }

    @PostMapping(name = "获取预报列表",path = "/getPredictionList")
    public ResponseData getPredictionList(@RequestBody MonitorParamDto monitorParamDto){
        monitorParamDto.setOrderBy(1);
        List<MonitorRetDto> monitorRetList = this.monitorService.getPredictionList(monitorParamDto);
        return ResponseData.success(monitorRetList);
    }

    @PostMapping(name = "点位下拉框",path = "/getPointPosition")
    public ResponseData getPointPosition(){
        List<MonitoringPoints> pointsList = this.monitoringPointsService.list();
        return ResponseData.success(pointsList);
    }

    @PostMapping(name = "监测类型下拉框",path = "/getMonitorType")
    public ResponseData getMonitorType(@RequestBody PointPosition position){
        QueryWrapper<PointPosition> pointPositionQueryWrapper = new QueryWrapper<>();
        pointPositionQueryWrapper.eq("points_id",position.getPointsId());
        List<PointPosition> list = this.pointPositionService.list(pointPositionQueryWrapper);
        return ResponseData.success(list);
    }

    @PostMapping(name = "获取点位信息",path = "/getPointInfo")
    public ResponseData getPointInfo(@RequestBody(required = false) MonitorParamDto monitorParamDto){
        List<PointPositionRetDto> positionRetDtoList = this.pointPositionService.getPointInfo(monitorParamDto);
        return ResponseData.success(positionRetDtoList);
    }

    @PostMapping(name = "设置点位阈值",path = "/editThreshold")
    public ResponseData editThreshold(@RequestBody PointThreshold pointThreshold){
        PointThreshold threshold = new PointThreshold();
        threshold.setMinValue(pointThreshold.getMinValue());
        threshold.setMaxValue(pointThreshold.getMaxValue());
        threshold.setUpdateTime(new Date());
        QueryWrapper<PointThreshold> thresholdQueryWrapper = new QueryWrapper<>();
        thresholdQueryWrapper.eq("point_id",pointThreshold.getPointId());
        PointThreshold p = this.pointThresholdService.getOne(thresholdQueryWrapper);
        if(ToolsUtils.isEmpty(p)){
            threshold.setPointId(pointThreshold.getPointId());
            this.pointThresholdService.save(threshold);
        } else {
            this.pointThresholdService.update(threshold,thresholdQueryWrapper);
        }
        return ResponseData.success();
    }

}